﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VSD.OneStopService.BLL;

public partial class Default : System.Web.UI.Page
{

    private ProfileStoreRepository ProfRepos;
    private List<ProfileStoreRepository.DistributedProfileInfo> ListProfileDist = new List<ProfileStoreRepository.DistributedProfileInfo>();
    private List<ProfileStoreRepository.WaitForProcessProfileInfo> ListProfileWaf = new List<ProfileStoreRepository.WaitForProcessProfileInfo>();
    private List<ProfileStoreRepository.ProcessingProfileInfo> ListProfileDangSL = new List<ProfileStoreRepository.ProcessingProfileInfo>();
    private List<ProfileStoreRepository.WFSProfileInfo> ListAllProf = new List<ProfileStoreRepository.WFSProfileInfo>();
    private List<ProfileStoreRepository.CompletedProfileInfo> ListCompProfiles = new List<ProfileStoreRepository.CompletedProfileInfo>();
    private List<ProfileStoreRepository.RepocessProfileInfo> ListRepProfiles = new List<ProfileStoreRepository.RepocessProfileInfo>() ;

    private bool IsManager = false;
    private bool IsDirector = false;
    private bool IsViewAll = false;
    private string userName = string.Empty;
    private int CurrentDeptID = -1;

    public string txtHSChoPhanXl = string.Empty;
    public string txtChoXL = string.Empty;
    public string txtHSDangXL = string.Empty;
    public string txtHSDaKyDuyet = string.Empty;
    public string txtHSChoDaKyDuyet = string.Empty;
    public string txtHSChoKyDuyet = string.Empty;
    public string txtHSHoanThanhXL = string.Empty;
    public string txtHSYeuCauXL = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        ProfRepos = new ProfileStoreRepository();

        if (ContextUser.CurrentUser != null && ContextUser.CurrentUser.DepartmentID.HasValue)
            CurrentDeptID = ContextUser.CurrentUser.DepartmentID.Value;
        else CurrentDeptID = -1;
        userName = ContextUser.CurrentUser.UserName;
        setPermissionExcute();
        if (!IsPostBack)
        {
            CuontNumber();
            cuontNumberHSHoanThanhXL();
        }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        FilterData();
    }
    public void setPermissionExcute()
    {
        this.panelRptProfile.Visible = false;
        try
        {
            this.panelRptProfile.Visible = (ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_COQUAN)) || (ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_PHONG_CHUYENMON));

        }
        catch { }
    }

    public void FilterData()
    {

        ListProfileDist = ProfRepos.GetWaitForDistributedProfiles(0).Take(5).ToList();

        if (this.ListProfileDist == null) return;

        rptProfiles.DataSource = ListProfileDist;
        rptProfiles.DataBind();

        //-----------------------------

        //ProfRepos = new ProfileStoreRepository();
        ListProfileWaf = ProfRepos.GetWaitForProcessingProfiles(CurrentDeptID, userName).Take(5).ToList();
        if (this.ListProfileWaf == null) return;

        this.rptHoSo.DataSource = ListProfileWaf;
        rptHoSo.DataBind();

        //-----------------------------
        //ProfRepos = new ProfileStoreRepository();
        ListProfileDangSL = ProfRepos.GetProcessingProfiles(CurrentDeptID, userName).Take(5).ToList();
        if (this.ListProfileDangSL == null) return;

        rptDangXL.DataSource = ListProfileDangSL;
        rptDangXL.DataBind();
    }

    public void CuontNumber()
    {
        int deptid = 0;
        //string userName = string.Empty;
        string resolver = string.Empty;
        bool IncludeReturning = false;

        ListProfileDist = ProfRepos.GetWaitForDistributedProfiles(0).ToList();

        for (int i = -1; i < ListProfileDist.Count; i++)
        {
            if (i >= 0)
            {
                txtHSChoPhanXl = "( " + Convert.ToString(i + 1) + " )";
            }
            else
                txtHSChoPhanXl = "( " + Convert.ToString(i + 1) + " )";
        }

        ListProfileWaf = ProfRepos.GetWaitForProcessingProfiles(CurrentDeptID, userName).ToList();
        for (int i = -1; i < ListProfileWaf.Count; i++)
        {
            if (i >= 0)
            {
                txtChoXL = "( " + Convert.ToString(i + 1) + " )";
            }
            else
                txtChoXL = "( " + Convert.ToString(i + 1) + " )";
        }

        ListProfileDangSL = ProfRepos.GetProcessingProfiles(CurrentDeptID, userName).ToList();
        for (int i = -1; i < ListProfileDangSL.Count; i++)
        {
            if (i >= 0)
            {
                txtHSDangXL = "( " + Convert.ToString(i + 1) + " )";
            }
            else
                txtHSDangXL = "( " + Convert.ToString(i + 1) + " )";
        }

        if (IsDirector || IsViewAll)
        {
            userName = (true ? ContextUser.UserName : string.Empty);
        }
        else if (IsManager || (ContextUser.GlobalOption.EnabledViewAll.HasValue && ContextUser.GlobalOption.EnabledViewAll.Value))
        {
            deptid = ContextUser.CurrentUser.DepartmentID.HasValue ? ContextUser.CurrentUser.DepartmentID.Value : 0;
            userName = string.Empty;
        }
        else
        {
            deptid = 0;
            resolver = ContextUser.UserName;
        }
        IncludeReturning = true;

        List<ProfileStoreRepository.SignedProfileInfo> list =
            ProfRepos.GetSignedProfiles(deptid, userName, resolver, IncludeReturning).ToList();
        if (deptid == 0)
        {
            list.AddRange(ProfRepos.GetSignedProfiles(0, ContextUser.UserName, null, IncludeReturning).ToList());
            list = list.Distinct(new DistinctProfile()).ToList();
        }

        for (int i = -1; i < list.Count; i++)
        {
            if (i >= 0)
            {
                txtHSDaKyDuyet = "( " + Convert.ToString(i + 1) + " )";
            }
            else
                txtHSDaKyDuyet = "( " + Convert.ToString(i + 1) + " )";
        }

        if (IsDirector || IsViewAll) //Nếu là vai trò lãnh đạo CQ
        {
            //int deptid = 0;
            ListAllProf = ProfRepos.GetWaitForSignedProfiles(CurrentDeptID, string.Empty, string.Empty).ToList();

            if (true)
                ListAllProf.RemoveAll(r => r.SignerUserName != ContextUser.UserName);
            ListAllProf = ListAllProf.FindAll(r => r.SignerUserName == ContextUser.UserName);
        }
        else if (IsManager || (ContextUser.GlobalOption.EnabledViewAll.HasValue && ContextUser.GlobalOption.EnabledViewAll.Value)) //Nếu là vai trò phong
        {
            if (ContextUser.CurrentUser.DepartmentID.HasValue)
                ListAllProf = ProfRepos.GetWaitForSignedProfiles(ContextUser.CurrentUser.DepartmentID.Value, string.Empty, string.Empty).ToList();
            //ListAllProf.AddRange(ProfRepos.GetWaitForSignedProfiles(0, ContextUser.UserName, null));
            ListAllProf = ListAllProf.Distinct(new DistinctProfilea()).ToList();
        }

        for (int i = -1; i < ListAllProf.Count; i++)
        {
            if (i >= 0)
            {
                txtHSChoKyDuyet = "( " + Convert.ToString(i + 1) + " )";
            }
            else
                txtHSChoKyDuyet = "( " + Convert.ToString(i + 1) + " )";
        }

    }

    public void cuontNumberHSHoanThanhXL()
    {
        int deptid = -1;
        bool HaveSuccess = true;
        bool HaveRejected = true;
        userName = ContextUser.UserName;
        deptid = CurrentDeptID;

        ServiceStatusCode.CodeValue[] sts;
        if (!HaveSuccess)
            sts = new ServiceStatusCode.CodeValue[1] { ServiceStatusCode.CodeValue.khong_dat };
        else if (!HaveRejected)
            sts = new ServiceStatusCode.CodeValue[1] { ServiceStatusCode.CodeValue.dat };
        else if (HaveRejected && HaveSuccess)
            sts = new ServiceStatusCode.CodeValue[2] { ServiceStatusCode.CodeValue.khong_dat, ServiceStatusCode.CodeValue.dat };
        else sts = new ServiceStatusCode.CodeValue[0];
        if (userName == "")
        {
            ListCompProfiles = ProfRepos.GetCompletedProfiles(deptid, string.Empty, sts).ToList();
        }
        else if (userName == "")
        {
            ListCompProfiles = ProfRepos.GetCompletedProfiles(0, string.Empty, sts).ToList();
        }
        else
            ListCompProfiles = ProfRepos.GetCompletedProfiles(0, userName, sts).ToList();

        for (int i = -1; i < ListCompProfiles.Count; i++)
        {
            if (i >= 0)
            {
                txtHSHoanThanhXL = "( " + Convert.ToString(i + 1) + " )";
            }
            else
                txtHSHoanThanhXL = "( " + Convert.ToString(i + 1) + " )";
        }

        ListRepProfiles = ProfRepos.GetReprocessProfiles(deptid, userName).ToList();
        for (int i = -1; i < ListRepProfiles.Count; i++)
        {
            if (i >= 0)
            {
                txtHSYeuCauXL = "( " + Convert.ToString(i + 1) + " )";
            }
            else
                txtHSYeuCauXL = "( " + Convert.ToString(i + 1) + " )";
        }
    }
    class DistinctProfile : IEqualityComparer<ProfileStoreRepository.SignedProfileInfo>
    {
        public bool Equals(ProfileStoreRepository.SignedProfileInfo x, ProfileStoreRepository.SignedProfileInfo y)
        {
            return x.ProfileCode.Equals(y.ProfileCode);
        }

        public int GetHashCode(ProfileStoreRepository.SignedProfileInfo obj)
        {
            return obj.ProfileCode.GetHashCode(); ;
        }
    }

    class DistinctProfilea : IEqualityComparer<ProfileStoreRepository.WFSProfileInfo>
    {
        public bool Equals(ProfileStoreRepository.WFSProfileInfo x, ProfileStoreRepository.WFSProfileInfo y)
        {
            return x.ProfileCode.Equals(y.ProfileCode);
        }

        public int GetHashCode(ProfileStoreRepository.WFSProfileInfo obj)
        {
            return obj.ProfileCode.GetHashCode(); ;
        }
    }
}